<?php


namespace app\extra\H5WeixinSDK\V3;

/**
 * 快捷登录接口类
 */
class JsApipublogin extends Wxpayclientpub {

    var $code; //code码，用以获取openid
    var $openid; //用户的openid
    var $parameters; //jsapi参数，格式为json
    var $prepay_id; //使用统一支付接口得到的预支付id
    var $curl_timeout; //curl超时时间

    function __construct() {
        //设置curl超时时间
        $this->curl_timeout = WxPayConfpublogin::CURL_TIMEOUT;
    }

    /**
     * 	作用：生成可以获得code的url
     */
    function createOauthUrlForCode($redirectUrl) {
        $urlObj["appid"]         = WxPayConfpublogin::APPID;
        $urlObj["redirect_uri"]  = $redirectUrl;
        $urlObj["response_type"] = "code";
        $urlObj["scope"]         = "snsapi_base";
        $urlObj["state"]         = "STATE" . "#wechat_redirect";
        $bizString               = $this->formatBizQueryParaMap($urlObj, false);
        return "https://open.weixin.qq.com/connect/oauth2/authorize?" . $bizString;
    }

    /**
     * 	作用：生成可以获得openid的url
     */
    function createOauthUrlForOpenid() {
        $urlObj["appid"]      = WxPayConfpublogin::APPID;
        $urlObj["secret"]     = WxPayConfpublogin::APPSECRET;
        $urlObj["code"]       = $this->code;
        $urlObj["grant_type"] = "authorization_code";
        $bizString            = $this->formatBizQueryParaMap($urlObj, false);
        return "https://api.weixin.qq.com/sns/oauth2/access_token?" . $bizString;
    }

    /**
     * 	作用：生成可以获得access_token的url
     */
    function createOauthUrlForaccesstoken($openid, $access_token) {
        $urlObj["openid"]       = $openid;
        $urlObj["access_token"] = $access_token;
        $urlObj["lang"]         = "zh_CN";
        $bizString              = $this->formatBizQueryParaMap($urlObj, false);
        return "https://api.weixin.qq.com/cgi-bin/user/info?" . $bizString;
    }

    /**
     * 	作用：通过curl向微信提交code，以获取openid
     */
    function getOpenid() {
        $url          = $this->createOauthUrlForOpenid();
        //初始化curl
        $ch           = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //运行curl，结果以jason形式返回
        $res          = curl_exec($ch);
        curl_close($ch);
        //取出openid
        $data         = json_decode($res, true);
		if(!isset($data['openid']) && rand(1,10)==5)
		{
			 \Our\Log::getInstance()->write('微信openid：' . var_export($data, true).'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], 'WXopenid.log');
		}

        $this->openid = $data['openid'];
        return $this->openid;
    }

    /**
     * 	作用：通过curl向微信提交code，以获取openid
     */
    function getOpenidall() {
        $url  = $this->createOauthUrlForOpenid();
        //初始化curl
        $ch   = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //运行curl，结果以jason形式返回
        $res  = curl_exec($ch);
        curl_close($ch);
        //取出openid
        $data = json_decode($res, true);

        return $data;
    }

    /**
     * 	作用：通过curl向微信提交code，以获取openid
     */
    function getOpeniduserinfo($openid) {
        $redis        = \Redis\Db4\WeixinTokenModel::getInstance();
        $access_token = $redis->find(WxPayConfpublogin::APPID); 
		 
        if (empty($access_token)) {
            $appid        = WxPayConfpublogin::APPID;
            $secret       = WxPayConfpublogin::APPSECRET;
			$token_json = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
		 
			$token_array = json_decode($token_json,true);
			$access_token = "";
			if(isset($token_array['access_token']))
			{
				$access_token = $token_array['access_token'];
				$redis->update(WxPayConfpublogin::APPID, $access_token,6000);
			} 
		}

        $url = $this->createOauthUrlForaccesstoken($openid, $access_token);

        //初始化curl
        $ch   = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //运行curl，结果以jason形式返回
        $res  = curl_exec($ch);
        curl_close($ch);
        //取出openid
        $data = json_decode($res, true);
		if(!isset($data['openid']) && rand(1,10)==5)
		{
			 \Our\Log::getInstance()->write('微信openid：' . var_export($data, true).'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], 'WXuserinfo.log');
		}


        return $data;
    }

    /**
     * 	作用：通过curl  jsapi
     */
    function getjsapistr($url) { 
        $appid  = WxPayConfpublogin::APPID;
        $secret = WxPayConfpublogin::APPSECRET;

        $redis        = \Redis\Db4\WeixinTokenModel::getInstance();
        $access_token = $redis->find(WxPayConfpublogin::APPID);
        if (empty($access_token)) {
            $appid        = WxPayConfpublogin::APPID;
            $secret       = WxPayConfpublogin::APPSECRET;
			$token_json = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
		 
			$token_array = json_decode($token_json,true);
			$access_token = "";
			if(isset($token_array['access_token']))
			{
				$access_token = $token_array['access_token'];
				$redis->update(WxPayConfpublogin::APPID, $access_token,6000);
			} 
        }
        $ticket = $redis->find(WxPayConfpublogin::APPID . "ticket");
        if (empty($ticket)) {
            //   $access_token = file_get_contents("http://weixin.pba.cn/api/getaccesstoken?appid=" . $appid . "&secret=" . $secret);
            $url2   = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $access_token . "&type=jsapi";
            //初始化curl
            $ch     = curl_init();
            //设置超时
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
            curl_setopt($ch, CURLOPT_URL, $url2);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            //运行curl，结果以jason形式返回
            $res    = curl_exec($ch);
            curl_close($ch);
            //取出openid
            $result = json_decode($res, true);
            if (!$result || $result["errcode"] != "0") {
                echo "get ticket error.";
                exit();
            }
            $ticket = $result['ticket'];
            $redis->update(WxPayConfpublogin::APPID . "ticket", $ticket,6000);
        } 
        $data['ticket'] = $ticket;

        $data['time'] = time();

        $url               = str_replace('?platform=wap', '', $url);
        $data['nonceStr']  = $this->createNoncestrs();
        $data['signature'] = sha1("jsapi_ticket=" . $data['ticket'] . "&noncestr=" . $data['nonceStr'] . "&timestamp=" . $data['time'] . "&url=" . $url);
        $data['appid']     = $appid;
        $data['url']       = $url;
        $data['str']       = "jsapi_ticket=" . $data['ticket'] . "&noncestr=" . $data['nonceStr'] . "&timestamp=" . $data['time'] . "&url=" . $url;
        return $data;
    }

    /**
     * 	作用：产生随机字符串，不长于32位
     */
    public function createNoncestrs($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str   = "";
        for ($i = 0; $i < $length; $i++) {
            $str.= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    /**
     * 	作用：设置prepay_id
     */
    function setPrepayId($prepayId) {
        $this->prepay_id = $prepayId;
    }

    /**
     * 	作用：设置code
     */
    function setCode($code_) {
        $this->code = $code_;
    }
 

    /**
     * 	作用：设置jsapi的参数
     */
    public function getParameters() {
        $jsApiObj["appId"]     = WxPayConfpublogin::APPID;
        $jsApiObj["timeStamp"] = strval(time());
        $jsApiObj["nonceStr"]  = $this->createNoncestr();
        $jsApiObj["package"]   = "prepay_id=$this->prepay_id";
        $jsApiObj["signType"]  = "MD5";
        $jsApiObj["paySign"]   = $this->getSign($jsApiObj);
        $this->parameters      = json_encode($jsApiObj);

        return $this->parameters;
    }

  

}
